package com.bj58.data.emailreports.controllers;

import java.io.File;
import java.io.FileInputStream;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;

import com.bj58.data.emailreports.model.TopJianliReportDetail;
import com.bj58.data.emailreports.model.TopJianliReportTop;
import com.bj58.data.emailreports.model.TopJianliReportTotal;
import com.bj58.data.emailreports.service.TopJianliService;
import com.bj58.data.emailreports.utils.DateUtil;
import com.bj58.data.emailreports.utils.EmailReportUtil;
import com.bj58.data.emailreports.utils.MailUtil;
import com.bj58.data.emailreports.utils.ResourcesUtil;
import com.bj58.data.emailreports.utils.SomePublicUtil;
import com.bj58.data.emailreports.utils.SubmitSms;
import com.bj58.data.emailreports.utils.TemplateMethodFactory;
import com.bj58.data.emailreports.utils.VelocityTools;
import com.bj58.groupbuy.service.locator.ServiceLocator;
import com.bj58.groupbuy.util.PathUtil;

public class TopJianliReportController {
	
	private TopJianliService topjianliService = ServiceLocator.get(TopJianliService.class);
	private SomePublicUtil  spu = new SomePublicUtil();
	private VelocityTools vTools = new VelocityTools();

	/**置顶简历日报
	 */
	public String topjianliReport(String searchDate)throws Exception{
		
		try {
			//velocity对象
			VelocityContext context = new VelocityContext();
			Object tools = TemplateMethodFactory.getInstance(VelocityTools.class);
			context.put("tools",tools);
			
			//读取模板内容
			String filepath =PathUtil.getWFNamespaceFolder()+"/emailtemp/TopJianliReport.html";
			ResourcesUtil.setCharset(Charset.forName("utf-8"));
			FileInputStream inputStream = new FileInputStream(new File(filepath));
			String templateContext = ResourcesUtil.readString2(inputStream);
			inputStream.close();
			
			Date  sDate = DateUtil.strToDate(searchDate, "yyyy-MM-dd");
			String nowDate = DateUtil.getStringDate(sDate,"yyyy-MM-dd");
			String lastDate = DateUtil.getStringDate(DateUtil.getDateBefore(sDate, 1),"yyyy-MM-dd");
			String lastWeek = DateUtil.getStringDate(DateUtil.getDateBefore(sDate, 7),"yyyy-MM-dd");
			context.put("nowDate",nowDate);
			context.put("lastDate",lastDate);
			context.put("lastWeek",lastWeek);
			
			//日期map
			Map<String,String> dateMap = new HashMap<String, String>();
			dateMap.put("nowDate",nowDate);
			dateMap.put("lastDate", lastDate);
			dateMap.put("lastWeek",lastWeek);
				
			double threshold = 0.6;
			double threshold1 = 0.2; //设置两个阈值，如50%，+-0.3，表示有50%的同环比超过阈值0.3
			EmailReportUtil eru = new EmailReportUtil();
			
			//置顶简历总体数据
			List<TopJianliReportTotal> topjlTotal = topjianliService.getTopJianliTotalData(nowDate,lastDate,lastWeek);
			List<String>  dicsList = spu.getECDicByAnnotation(TopJianliReportTotal.class);
			//List<String> allDataList = erutil.conformDataTypeOne(topjlTotal, TopJianliReportTotal.class, null, dateMap, "statDate",dicsList);
			List<String> allDataList = eru.conDataTypeOneWithWarn(topjlTotal, TopJianliReportTotal.class, null, dateMap, "statDate",dicsList,threshold1);
			context.put("allDataList",allDataList);		
			
			//置顶简历详细数据
			List<TopJianliReportDetail> topjlDetail = topjianliService.getTopJianliDetailData(nowDate,lastDate,lastWeek);
			List<String>  dicsList2 = spu.getECDicByAnnotation(TopJianliReportDetail.class);
			//List<String> detailDataList = erutil.conformDataTypeOne(topjlDetail, TopJianliReportDetail.class, null, dateMap, "statDate",dicsList2);
			List<String> detailDataList = eru.conDataTypeOneWithWarn(topjlDetail, TopJianliReportDetail.class, null, dateMap, "statDate",dicsList2,threshold1);
			context.put("detailDataList",detailDataList);		
			
			//Top二级类
			List<TopJianliReportTop> topjlTop=topjianliService.getTopJianliTopData(nowDate);
			
			//把10个对象放到String里
			List<String>  showValuesList = new ArrayList<String>();
			
			int n=10;
			for(TopJianliReportTop vo : topjlTop){
				
				if(n==0)break;			
				StringBuffer  showValues = new StringBuffer(); 
				
				showValues.append(vo.getDispCate2()+";");
				showValues.append(vo.getTopAmortize()+";");
				showValues.append(vo.getCashAmortize()+";");			
				showValues.append(vo.getTopCost()+";");
				showValues.append(vo.getCashCost()+";");
				showValues.append(vo.getTopCus()+";");
				showValues.append(vo.getCostCus()+";");
							
				showValuesList.add(showValues.toString());			
				n--;
			}
			
			context.put("showValuesList",showValuesList);		

			//Top城市
			List<TopJianliReportTop> topjlTopCity=topjianliService.getTopJianliTopCityData(nowDate);
			
			//把10个对象放到String里
			List<String>  showValuesList1 = new ArrayList<String>();
			
			n=10;
			for(TopJianliReportTop vo : topjlTopCity){
				
				if(n==0)break;		
				StringBuffer  showValues = new StringBuffer(); 
				
				showValues.append(vo.getDispCityName()+";");
				showValues.append(vo.getTopAmortize()+";");
				showValues.append(vo.getCashAmortize()+";");			
				showValues.append(vo.getTopCost()+";");
				showValues.append(vo.getCashCost()+";");
				showValues.append(vo.getTopCus()+";");
				showValues.append(vo.getCostCus()+";");
				
				showValuesList1.add(showValues.toString());	
				n--;
			}					
			context.put("showValuesList1",showValuesList1);		
				

			VelocityEngine velocity  = new VelocityEngine();
			Velocity.setProperty("input.encoding", "utf-8");
			Velocity.setProperty("output.encoding", "utf-8");
			String content = templateContext;
			StringWriter out = new StringWriter();
			try {
					velocity.evaluate(context, out, "", content);
			} catch (Exception e) {            
				e.printStackTrace();
			}
			content = out.toString();
			out.close();
			
			//阈值预警
			if(eru.getAbnomalTd()*1.0/eru.getTd() > threshold){
				String head = "当前阈值:"+vTools.tranRateTwo(threshold)+",+-"+threshold1+"<br>";
				head += "异常数据比例:"+vTools.compRateDirect(eru.getAbnomalTd(),eru.getTd())+"<br>";
				MailUtil.javaMailSend("【预警】简历置顶日报" +searchDate , head+content, "guoxue@58.com;zhaoxiang@58.com;huangliang@58.com",null);
				Map<String,String> sendMap = new HashMap<String, String>();
	        	sendMap.put("15726690526", "【预警】简历置顶日报" +searchDate);
	        	SubmitSms.submitMsg(sendMap);
				return "";
			}

			
			return content;
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	
	public static void main(String[] args) {
		TopJianliReportController  wk = new TopJianliReportController();
		String searchDate = "2016-03-21";
		String context;
		try {
			context = wk.topjianliReport(searchDate);
			if(!"".equals(context)){
				MailUtil.javaMailSend("简历置顶日报测试" + searchDate, context, "guoxue@58.com",null);			
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
